﻿using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Microsoft.EntityFrameworkCore;
using shenghua.Common.CustomValueType;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Commodity.Commodities
{
    public class Commodity : Entity, IFullAudited, IExtendableObject
    {
        public string Name { get; set; }

        /// <summary>
        /// 商品库ID
        /// </summary>
        public int CommodityWareId { get; set; }

        /// <summary>
        /// 商品类别
        /// </summary>
        public ValueArray<string> Classes { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public ValueArray<string> Tags { get; set; }

        /// <summary>
        /// 上架
        /// </summary>
        public bool Shelved { get; set; }

        /// <summary>
        /// 推荐
        /// </summary>
        public bool IsRecommended { get; set; }

        /// <summary>
        /// 是否启用购物选项
        /// </summary>
        public bool IsEnabledShoppingOption { get; set; }

        /// <summary>
        /// 是否可会员折扣
        /// </summary>
        public bool Discountable { get; set; }

        /// <summary>
        /// 是否独立会员级别折扣
        /// </summary>
        public bool IsDiscountPerLevel { get; set; }

        /// <summary>
        /// 购物相关信息
        /// </summary>
        public CommodityShoppingInfoValueType ShoppingInfo { get; set; }

        /// <summary>
        /// 勾选的购物选项参数
        /// </summary>
        public ValueArray<string> SelectedShoppingOptionParameters { get; set; }
        
        /// <summary>
        /// 商品描述
        /// </summary>
        public string Description { get; set; }

        public long? CreatorUserId { get; set; }

        public DateTime CreationTime { get; set; }

        public string ExtensionData { get; set; }

        /// <summary>
        /// 针对会员每一级别的折扣
        /// </summary>
        public Dictionary<int, double> Discounts
        {
            get
            {
                return this.GetData<Dictionary<int, double>>("discounts");
            }
        }

        public void SetDiscounts(Dictionary<int, double> discounts)
        {
            this.SetData("discounts", discounts);
        }

        /// <summary>
        /// 产品规格参数集
        /// </summary>
        public Dictionary<string, string> SpecificationParameters
        {
            get
            {
                return this.GetData<Dictionary<string, string>>("specificationparameters");
            }
        }

        public long? LastModifierUserId { get; set; }
        public DateTime? LastModificationTime { get; set; }
        public long? DeleterUserId { get; set; }
        public DateTime? DeletionTime { get; set; }
        public bool IsDeleted { get; set; }

        public void SetSpecificationParameters(Dictionary<string, string> parameters)
        {
            this.SetData("specificationparameters", parameters);
        }

        public Commodity()
        {
            Tags = new ValueArray<string>();
            ShoppingInfo = new CommodityShoppingInfoValueType();
            SetDiscounts(new Dictionary<int, double>());
            SelectedShoppingOptionParameters = new ValueArray<string>();
        }
    }

    [Owned]
    public class CommodityShoppingInfoValueType
    {

        public Money Price { get; set; }
        public Money MinPrice { get; set; }
        public Money MaxPrice { get; set; }

        /// <summary>
        /// 购买单个商品可获得的积分值
        /// </summary>
        public int XPValue { get; set; }

        /// <summary>
        /// 库存量
        /// </summary>
        public int Inventory { get; set; }

        /// <summary>
        /// 最小限购数量，大于等于1
        /// </summary>
        public int MinLimitCount { get; set; }

        /// <summary>
        /// 最大限购数量,0表示不限购
        /// </summary>
        public int MaxLimitCount { get; set; }

        public string Picture { get; set; }

        public ValueArray<string> Pictures { get; set; }

        /// <summary>
        /// 销量
        /// </summary>
        public int SalesVolume { get; set; }

        public string GetValidPicture(CommodityShoppingOption commodityShoppingOption = null)
        {
            if(commodityShoppingOption != null)
            {
                if (commodityShoppingOption.Pictures.Items.Count > 0)
                {
                    return commodityShoppingOption.Pictures.Items[0];
                }
            }

            if(string.IsNullOrWhiteSpace(Picture))
            {
                return Pictures.Items.FirstOrDefault();
            }

            return Picture;
        }

        public CommodityShoppingInfoValueType()
        {
            Price = new Money();
            MinPrice = new Money();
            MaxPrice = new Money();
            Pictures = new ValueArray<string>();
        }

        public bool IsValidCount(int count)
        {
            if (count >= MinLimitCount && count <= MaxLimitCount)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
